//
//  WJWelcomeView.swift
//  WJWeibo
//
//  Created by Jie on 15/12/18.
//  Copyright © 2015年 周文杰. All rights reserved.
//

import UIKit

class WJWelcomeView: UIView {
    
    private var iconBottomCons : NSLayoutConstraint?

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }


    
    
    
    
    
    
    //MARK: - 加载控件
    func prepareUI() {
        addSubview(bagView)
        addSubview(iconView)
        addSubview(textLab)
        
        //关闭Autoresizing
        bagView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        textLab.translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束
        //bagView
        //宽
        addConstraint(NSLayoutConstraint(item: bagView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        //高
        addConstraint(NSLayoutConstraint(item: bagView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        //iconView
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -150))
        
        iconBottomCons =  constraints.last
        
        
        //textLab
        addConstraint(NSLayoutConstraint(item: textLab, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: textLab, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        
    }
    
    
    //MARK: - 头像动画
    func starIconVieWAnim() {
        // 当使用自动约束时,不要去修改view的frame bounds center, 修改约束
        iconBottomCons?.constant = -UIScreen.mainScreen().bounds.height - iconBottomCons!.constant

        
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //使用此方法强制立即进行layout
            self.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.textLab.alpha = 1
                    }, completion: { (_) -> Void in
                        //回到首页
                })
            
                
        }
    }
    
    
    
    
    
    //MARK: - 懒加载控件
    //背景图片
    private lazy var bagView : UIImageView = {
        let img = UIImageView(image: UIImage(named: "ad_background"))
        return img
    }()
    
    //头像
    private lazy var iconView : UIImageView = {
        let img = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置圆角
        img.layer.cornerRadius = 42.5
        //截取
        img.layer.masksToBounds = true
        return img
    }()
    
    //字
    private lazy var textLab : UILabel = {
        let txt = UILabel()
        txt.text = "欢迎回来"
        txt.textColor = UIColor.darkGrayColor()
        txt.alpha = 0
        txt.sizeToFit()
        return txt
    }()
    
}
